package com.itheima.ClientApp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class MainApp {
    private static final String IP = "127.0.0.1";
    private static final Integer PORT = 8888;
    private static final Logger LOGGER = LoggerFactory.getLogger(MainApp.class);
    private static final Scanner SC = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("*************************************************************");
            System.out.println("*\t1.添加学员\t2.修改学员\t3.删除学员\t4.查询学员\t5.退出 \t*");
            System.out.println("*************************************************************");
            String s = SC.nextLine();
            switch (s) {
                case "1":
                    addStudent(SC);
                    break;
                case "2":
                    updateStudent(SC);
                    break;
                case "3":
                    deleteStudent(SC);
                    break;
                case "4":
                    findStudent(SC);
                    break;
                case "5":
                    LOGGER.info("[客户端]用户退出!");
                    System.exit(0);
                default:
                    LOGGER.warn("[客户端]输入选项非法,请重新输入:");
                    break;
            }
        }

    }

    //删除
    private static void deleteStudent(Scanner sc) {
        Socket socket = getConnection();
        if (socket != null) {
            System.out.println("请输入要删除的学生id:");
            String id = sc.nextLine();
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                bw.write("[3]"+id);
                bw.flush();
                //提示服务器写入完毕
                socket.shutdownOutput();
                System.out.println(br.readLine());
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    //修改
    private static void updateStudent(Scanner sc) {
        Socket socket = getConnection();
        if (socket != null) {
            LOGGER.info("客户端已连接服务器.");
            System.out.println("请输入要修改的学生id:");
            String oldId = sc.nextLine();
            System.out.println("请输入修改后的学生姓名:");
            String newName = sc.nextLine();
            System.out.println("请输入修改后的学生性别:");
            String newSex = sc.nextLine();
            System.out.println("请输入修改后的学生年龄");
            String newAge = sc.nextLine();
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                bw.write("[2]" + oldId + "," + newName + "," + newSex + "," + newAge);
                bw.flush();
                //提示服务器写入完毕
                socket.shutdownOutput();
                //输出服务器对修改请求的回应
                System.out.println(br.readLine());
                socket.close();
            } catch (IOException e) {
                LOGGER.error("[客户端]获取SOCKET流对象时候出现错误.");
            }

        } else {
            LOGGER.warn("[客户端]创建Socket对象失败.");
        }


    }

    //查找
    private static void findStudent(Scanner sc) {
        Socket socket = getConnection();
        if (socket != null) {
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                bw.write("[4]");
                bw.flush();
                //提示服务器写入完毕
                socket.shutdownOutput();
                String str;
                while ((str = br.readLine()) != null) {
                    System.out.println(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            LOGGER.warn("[客户端]创建Socket对象失败.");
        }
    }

    private static void addStudent(Scanner sc) {
        Socket socket = getConnection();
        if (socket != null) {
            LOGGER.info("客户端已连接服务器.");
            System.out.println("请输入学生姓名:");
            String name = sc.nextLine();
            System.out.println("请输入学生性别:");
            String sex = sc.nextLine();
            System.out.println("请输入学生年龄:");
            String age = sc.nextLine();
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                bw.write("[1]" + name + "," + sex + "," + age);
                bw.flush();
                //提示服务器写入完毕
                socket.shutdownOutput();
                System.out.println(br.readLine());
                socket.close();
            } catch (IOException e) {
                LOGGER.error("[客户端]获取SOCKET流对象时候出现错误.");
            }

        } else {
            LOGGER.warn("[客户端]创建Socket对象失败.");
        }


    }

    //获取连接
    private static Socket getConnection() {
        Socket socket = null;
        try {
            socket = new Socket(IP, PORT);
        } catch (IOException e) {
            LOGGER.error("[客户端]客户端创建连接时发生错误!!!");
        }
        return socket;
    }
}
